package per.lance.easytomcat.util;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class FileUtil {

    public static byte[] toByteArray(File file) throws IOException {
        File f = file;
        if (!f.exists() && !f.getName().contains(".ico")) {
            throw new FileNotFoundException("file not exists");
        }
        ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length());
        BufferedInputStream in = null;
        try {
            in = new BufferedInputStream(new FileInputStream(f));
            int buf_size = 1024;
            byte[] buffer = new byte[buf_size];
            int len = 0;
            while (-1 != (len = in.read(buffer, 0, buf_size))) {
                bos.write(buffer, 0, len);
            }
            return bos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            bos.close();
        }
    }

    public static HashMap<String, String> getRedirectProperties(String proPath) throws IOException {
        Properties properties = new Properties();
        InputStream in = FileUtil.class.getClassLoader().getResourceAsStream(proPath);
        properties.load(in);
        return new HashMap<String, String>((Map) properties);
    }
}
